package com.easy.action;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.easy.bean.LayData;
import com.easy.bean.Repair;
import com.easy.service.Service;
import com.easy.service.impl.RepairService;

@WebServlet("/repair/*")
public class RepairServlet extends HttpServlet{
	private Service repairS=new RepairService();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uri=req.getRequestURI();
		//uri:  /hotelms/user/list
		uri=uri.substring(uri.lastIndexOf("/")+1);
		Map params = (Map)req.getAttribute("params");
		
		
		if("list".equals(uri)) {
			List<Repair> list=repairS.queryList(params);
			int count=repairS.queryCount(params);
			//封装成LayData对象
			//因为LayUI需要固定的格式来解析数据
			LayData lay=new LayData(count,list);
			//将lay对象转化成json数据,将json数据返回给前台
			String json=JSON.toJSONString(lay);
			resp.getWriter().write(json);
			return;
		}else if("add".equals(uri)) {
			//获取数据,将参数传给service,service交给dao访问数据库
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			int repair_id = Integer.parseInt(req.getParameter("repair_id"));
			int r_id = Integer.parseInt(req.getParameter("r_id"));
			int resp_id = Integer.parseInt(req.getParameter("resp_id"));
			int req_id = Integer.parseInt(req.getParameter("req_id"));
			String question = req.getParameter("question");
			String feedback = req.getParameter("feedback");
			String rp_state = req.getParameter("rp_state");
			try {
				Date req_date = format.parse(req.getParameter("req_date"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				Date resp_date = format.parse(req.getParameter("resp_date"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int count = repairS.insert(params);
			resp.getWriter().write(count);
			return;
		}else if("del".equals(uri)) {
			
			
			//获取数据,将参数传给service,service交给dao访问数据库
			int repair_id = Integer.parseInt(req.getParameter("repair_id"));
			int count = repairS.delete(params);
			resp.getWriter().write(count);
			return;
		}else if("upd".equals(uri)) {
			
			
			
			//获取数据,将参数传给service,service交给dao访问数据库
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			int repair_id = Integer.parseInt(req.getParameter("repair_id"));
			int r_id = Integer.parseInt(req.getParameter("r_id"));
			int resp_id = Integer.parseInt(req.getParameter("resp_id"));
			int req_id = Integer.parseInt(req.getParameter("req_id"));
			String question = req.getParameter("question");
			String feedback = req.getParameter("feedback");
			String rp_state = req.getParameter("rp_state");
			try {
				Date req_state = format.parse(req.getParameter("req_date"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				Date resp_date = format.parse(req.getParameter("resp_date"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int count = repairS.update(params);
			resp.getWriter().write(count);
			return;
		}
	}
}
